tool
extends PanelContainer


signal changed


onready var name_edit = find_node("Name")
onready var start_edit = find_node("Start")
onready var end_edit = find_node("End")
onready var speed_edit = find_node("Speed")
onready var loop_edit = find_node("Loop")
onready var changed_timer = $ChangedTimer



#==================================================
#   Set/Get
#==================================================
func get_data():
	return {
		name = name_edit.text,
		start = start_edit.value,
		end = end_edit.value,
		speed = speed_edit.value,
		loop = loop_edit.pressed,
	}


func set_data(value : Dictionary):
	if name_edit == null:
		yield(self, "ready")
	var node_map = {
		name = name_edit,
		start = start_edit,
		end = end_edit,
		speed = speed_edit,
		loop = loop_edit,
	}
	for key in value:
		if node_map.has(key):
			var node = node_map[key]
			if node is SpinBox:
				node.value = int(value[key])
			elif node is LineEdit:
				node.text = str(value[key])
			elif node is Button:
				node.pressed = bool(value[key])


func get_anim_name() -> String:
	return name_edit.text



#==================================================
#   内置方法
#==================================================
func _ready():
	connect_control_node(self)
	set_container_mouse_filter(self)
	self.mouse_filter = Control.MOUSE_FILTER_STOP
	start_edit.get_line_edit().placeholder_text = "Start"
	end_edit.get_line_edit().placeholder_text = "End"
	speed_edit.get_line_edit().placeholder_text = "Speed"


func get_drag_data(position):
	var data = get_data()
	var label = Label.new()
	label.text = (data.name if data.name != "" else "[None Animation Name]")
	set_drag_preview(label)
	return self


func can_drop_data(position, data):
	return data is get_script() && data != self


func drop_data(position, data):
	var parent = data.get_parent()
	# 是否放到节点的节点靠下位置
	var is_down = (position.y > self.rect_size.y / 2)
	# 上面拖到下面会跑到放到节点的之后
	if data.get_index() < self.get_index():
		if is_down:
			parent.move_child(data, self.get_index())
		else:
			parent.move_child(data, self.get_index() - 1)
	
	# 下面拖到上面会跑到放到节点的之前
	else:
		if is_down:
			parent.move_child(data, self.get_index()+1)
		else:
			parent.move_child(data, self.get_index())
	



#==================================================
#   自定义方法
#==================================================
func connect_control_node(parent: Node):
	for child in parent.get_children():
		if child is Control:
			child.connect("focus_entered", self, "_on_LineEdit_focus_entered", [child])
			child.connect("focus_exited", self, "_on_LineEdit_focus_exited", [child])
		if child is SpinBox:
			child.connect("value_changed", self, "_on_Control_changed")
		elif child is BaseButton:
			child.connect("toggled", self, "_on_Control_changed")
		
	for child in parent.get_children():
		connect_control_node(child)


##  设置 Container 类型节点的 mouse_filter 属性
## @parent  
func set_container_mouse_filter(parent: Node):
	for child in parent.get_children():
		if child is Container:
			child.mouse_filter = Control.MOUSE_FILTER_IGNORE
	for child in parent.get_children():
		set_container_mouse_filter(child)



#==================================================
#   连接信号
#==================================================
func _on_Remove_pressed():
	queue_free()


func _on_LineEdit_focus_entered(node: Control):
	yield(get_tree().create_timer(0.01), "timeout")
	if node is SpinBox:
		node.get_line_edit().select_all()
	emit_signal("focus_entered")


func _on_LineEdit_focus_exited(node: Control):
	if node is SpinBox:
		node.get_line_edit().select(0,0)
	emit_signal("focus_exited")


func _on_Control_changed(data):
	# 间隔 0.2 秒再发出改变信号，防止修改太快造成发送信号太多
	changed_timer.stop()
	changed_timer.start()


func _on_ChangedTimer_timeout():
	emit_signal("changed")
